<?php
/**
 * Static content controller.
 *
 * This file will render views from views/pages/
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */
App::uses('AppController', 'Controller');

/**
 * Static content controller
 *
 * Override this controller by placing a copy in controllers directory of an application
 *
 * @package       app.Controller
 * @link http://book.cakephp.org/2.0/en/controllers/pages-controller.html
 */
class WidgetController extends AppController
{
    public $helpers = array('Html', 'Form');

    /**
     * This controller does not use a model
     *
     * @var array
     */
    public $uses = array(
        'Page',
        'WidgetType'
    );

    /**
     * Displays a view
     *
     * @param mixed What page to display
     * @return void
     * @throws NotFoundException When the view file could not be found
     *    or MissingViewException in debug mode.
     */
    public function add()
    {
        $widgets = $this->WidgetType->find('all');

        $this->set('widgets', $widgets);
        $this->layout = false;
    }

    function create_equals($id = 0)
    {
        return function ($test) use ($id) {
            return $test == $id;
        };
    }

    public function category($name, $idcat, $idsubcat = null)
    {
        $this->set('id_cat', $idcat);
        if (!$idcat) {
            throw new NotFoundException(__('Invalid page'));
        }
        if ($idsubcat == null)
            $page = $this->Page->findByIdCat($idcat);
        else
            $page = $this->Page->findByIdSubcat($idsubcat);

        $this->set('id_sub_cat', $page ? $page['Page']['id_subcat'] : null);
        $this->set('page', $page ? $page : $this->Category->findById($idcat));
        $this->render('view');
    }

    public function subcategory($name, $idcat, $idsubcat)
    {
        $this->set('id_sub_cat', $idsubcat);
        if (!$idcat) {
            throw new NotFoundException(__('Invalid page'));
        }
        $page = $this->Page->findByIdSubcat($idsubcat);

        $this->set('id_cat', $idcat);
        $this->set('page', $page ? $page : null);
        $this->render('view');
    }

    public function getCats()
    {
        $cats = $this->Category->find('all', array('conditions' => array('Category.id_parent' => null), 'order' => array('Category.id' => 'asc')));
        return $cats;
    }

    public function getSubCats()
    {
        $subcats = $this->Category->findAllByIdParent($this->request->query['idcat'], null, array('Category.id' => 'asc'));
        return $subcats;
    }
}
